<!--
Copyright (c) 2010 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
 -->
<html>
<head>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>

<script>

var gl;
var fbo;
var depthBuffer;
var stencilBuffer;
var depthStencilBuffer;
var colorBuffer;
var width = 2;
var height = 2;

function testAttachment(attachment, buffer, isConflicted)
{
    shouldBeNonNull("fbo = gl.createFramebuffer()");
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, colorBuffer);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, buffer);
    glErrorShouldBe(gl, gl.NO_ERROR);
    if (isConflicted) {
        shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_UNSUPPORTED");
        gl.clear(gl.COLOR_BUFFER_BIT);
        glErrorShouldBe(gl, gl.INVALID_FRAMEBUFFER_OPERATION);
        gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(width * height * 4));
        glErrorShouldBe(gl, gl.INVALID_FRAMEBUFFER_OPERATION);
    }
}

function testAttachments(attachment0, buffer0, attachment1, buffer1, isConflicted)
{
    shouldBeNonNull("fbo = gl.createFramebuffer()");
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, colorBuffer);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment0, gl.RENDERBUFFER, buffer0);
    glErrorShouldBe(gl, gl.NO_ERROR);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment1, gl.RENDERBUFFER, buffer1);
    glErrorShouldBe(gl, gl.NO_ERROR);
    if (isConflicted)
        shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_UNSUPPORTED");
}

function testColorRenderbuffer(internalformat)
{
    shouldBeNonNull("colorBuffer = gl.createRenderbuffer()");
    gl.bindRenderbuffer(gl.RENDERBUFFER, colorBuffer);
    gl.renderbufferStorage(gl.RENDERBUFFER, internalformat, width, height);
    glErrorShouldBe(gl, gl.NO_ERROR);
    testAttachment(gl.COLOR_ATTACHMENT0, colorBuffer, false);
}

function testDepthStencilRenderbuffer()
{
    shouldBeNonNull("depthStencilBuffer = gl.createRenderbuffer()");
    gl.bindRenderbuffer(gl.RENDERBUFFER, depthStencilBuffer);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height);
    glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBe("gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_WIDTH)", "width");
    shouldBe("gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_HEIGHT)", "height");
    shouldBe("gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_INTERNAL_FORMAT)", "gl.DEPTH_STENCIL");
    shouldBe("gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_RED_SIZE)", "0");
    shouldBe("gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_GREEN_SIZE)", "0");
    shouldBe("gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_BLUE_SIZE)", "0");
    shouldBe("gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_ALPHA_SIZE)", "0");
    shouldBeTrue("gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_DEPTH_SIZE) > 0");
    shouldBeTrue("gl.getRenderbufferParameter(gl.RENDERBUFFER, gl.RENDERBUFFER_STENCIL_SIZE) > 0");
    glErrorShouldBe(gl, gl.NO_ERROR);
    testAttachment(gl.DEPTH_STENCIL_ATTACHMENT, depthStencilBuffer, false);
}

description("Test framebuffer object attachment behaviors");

debug("Create renderbuffers");
shouldBeNonNull("gl = create3DContext()");
shouldBeNonNull("colorBuffer = gl.createRenderbuffer()");
gl.bindRenderbuffer(gl.RENDERBUFFER, colorBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, width, height);
glErrorShouldBe(gl, gl.NO_ERROR);
shouldBeNonNull("depthBuffer = gl.createRenderbuffer()");
gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
glErrorShouldBe(gl, gl.NO_ERROR);
shouldBeNonNull("stencilBuffer = gl.createRenderbuffer()");
gl.bindRenderbuffer(gl.RENDERBUFFER, stencilBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.STENCIL_INDEX8, width, height);
glErrorShouldBe(gl, gl.NO_ERROR);
shouldBeNonNull("depthStencilBuffer = gl.createRenderbuffer()");
gl.bindRenderbuffer(gl.RENDERBUFFER, depthStencilBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height);
glErrorShouldBe(gl, gl.NO_ERROR);

debug("Attach depth using DEPTH_ATTACHMENT");
testAttachment(gl.DEPTH_ATTACHMENT, depthBuffer, false);
debug("Attach depth using STENCIL_ATTACHMENT");
testAttachment(gl.STENCIL_ATTACHMENT, depthBuffer, true);
debug("Attach depth using DEPTH_STENCIL_ATTACHMENT");
testAttachment(gl.DEPTH_STENCIL_ATTACHMENT, depthBuffer, true);
debug("Attach stencil using STENCIL_ATTACHMENT");
testAttachment(gl.STENCIL_ATTACHMENT, stencilBuffer, false);
debug("Attach stencil using DEPTH_ATTACHMENT");
testAttachment(gl.DEPTH_ATTACHMENT, stencilBuffer, true);
debug("Attach stencil using DEPTH_STENCIL_ATTACHMENT");
testAttachment(gl.DEPTH_STENCIL_ATTACHMENT, stencilBuffer, true);
debug("Attach depthStencil using DEPTH_STENCIL_ATTACHMENT");
testAttachment(gl.DEPTH_STENCIL_ATTACHMENT, depthStencilBuffer, false);
debug("Attach depthStencil using DEPTH_ATTACHMENT");
testAttachment(gl.DEPTH_ATTACHMENT, depthStencilBuffer, true);
debug("Attach depthStencil using STENCIL_ATTACHMENT");
testAttachment(gl.STENCIL_ATTACHMENT, depthStencilBuffer, true);

debug("Attach depth, then stencil, causing conflict");
testAttachments(gl.DEPTH_ATTACHMENT, depthBuffer, gl.STENCIL_ATTACHMENT, stencilBuffer, true);
debug("Attach stencil, then depth, causing conflict");
testAttachments(gl.STENCIL_ATTACHMENT, stencilBuffer, gl.DEPTH_ATTACHMENT, depthBuffer, true);
debug("Attach depth, then depthStencil, causing conflict");
testAttachments(gl.DEPTH_ATTACHMENT, depthBuffer, gl.DEPTH_STENCIL_ATTACHMENT, depthStencilBuffer, true);
debug("Attach depthStencil, then depth, causing conflict");
testAttachments(gl.DEPTH_STENCIL_ATTACHMENT, depthStencilBuffer, gl.DEPTH_ATTACHMENT, depthBuffer, true);
debug("Attach stencil, then depthStencil, causing conflict");
testAttachments(gl.DEPTH_ATTACHMENT, depthBuffer, gl.DEPTH_STENCIL_ATTACHMENT, depthStencilBuffer, true);
debug("Attach depthStencil, then stencil, causing conflict");
testAttachments(gl.DEPTH_STENCIL_ATTACHMENT, depthStencilBuffer, gl.STENCIL_ATTACHMENT, stencilBuffer, true);

debug("Attach color renderbuffer with internalformat == RGBA4");
testColorRenderbuffer(gl.RGBA4);

debug("Attach color renderbuffer with internalformat == RGB5_A1");
testColorRenderbuffer(gl.RGB5_A1);

debug("Attach color renderbuffer with internalformat == RGB565");
testColorRenderbuffer(gl.RGB565);

debug("Create and attach depthStencil renderbuffer");
testDepthStencilRenderbuffer();

successfullyParsed = true;
</script>

<script src="../resources/js-test-post.js"></script>
</body>
</html>
